home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / game / shoot / Orbit_src.lha / Orbit / source / keyboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-04  |  12.8 KB  |  710 lines

  1. /*
  2.     Amiga port by Oliver Gantert
  3.  
  4.     27.04.2000 - fixed some compiler warnings
  5.     28.04.2000 - fixed a problem with key states
  6.                  left pitch roll assigned to <,>
  7.                  right pitch roll assigned to <.>
  8.                  cursor movement more precise (0.5 instead of 1.0)
  9.     30.04.2000 - added FreeJoy() call to quit keys
  10. */
  11. /*
  12.  
  13. ORBIT, a freeware space combat simulator
  14. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  15.  
  16. This program is free software; you can redistribute it and/or
  17. modify it under the terms of the GNU General Public License
  18. as published by the Free Software Foundation; either version 2
  19. of the License, or (at your option) any later version.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  29.  
  30. */
  31.  
  32. #include "orbit.h"
  33.  
  34. char key[256], spec[256];
  35.  
  36. static void Key (unsigned char k, int x, int y)
  37. {
  38.   /* Are we reading in some text? */
  39.   if (text.yes)
  40.   {
  41.     if ( ((k == 127) || (k == 8)) && (text.index > 0) )  /* Backspace */
  42.     {
  43.       text.index--;
  44.       text.buf[text.index] = 0;
  45.       Mprint ("%s%s", text.prompt, text.buf);
  46.       return;
  47.     }
  48.     if (k == 27) /* Escape */
  49.     {
  50.       text.yes = 0;
  51.       Mprint (" ");
  52.       return;
  53.     }
  54.     else if (k == 13) /* Return */
  55.     {
  56.       Mprint (" ");
  57.       text.func();
  58.       text.yes = 0;
  59.       return;
  60.     }
  61.     else
  62.     {
  63.       /* Add character to buffer */
  64.       if (text.index >= (TEXTSIZE-1))
  65.       {
  66.         text.func();
  67.         text.yes = 0;
  68.         return;
  69.       }
  70.       text.buf[text.index++] = k;
  71.       text.buf[text.index] = 0;
  72.       Mprint ("%s%s", text.prompt, text.buf);
  73.       return;
  74.     }
  75.   }
  76.  
  77.   if (state == STATE_DEAD1) return;
  78.  
  79.   if (state == STATE_DEAD2)
  80.   {
  81.     state = STATE_NORMAL;
  82.     Mprint (" ");
  83.     return;
  84.   }
  85.  
  86.   if (paused)
  87.   {
  88.     paused = 0;
  89.     Mprint ("");
  90.     if ( (state != STATE_LOADGAME) ||
  91.     (k < '0') ||
  92.     (k >= '0'+nsaves) )
  93.     {
  94.       if (state == STATE_LOADGAME) state = STATE_NORMAL;
  95.       return;
  96.     }
  97.   }
  98.   key[k] = 0x81;
  99. }
  100.  
  101. static void KeyUp (unsigned char k, int x, int y)
  102. {
  103.   key[k] &= 0x7f;
  104.  
  105.   if (k == 'a') key['A'] &= 0x7f;
  106.   if (k == 'z') key['Z'] &= 0x7f;
  107.   if (k == 'A') key['a'] &= 0x7f;
  108.   if (k == 'Z') key['z'] &= 0x7f;
  109. }
  110.  
  111. static void Spec (int k, int x, int y)
  112. {
  113.   if (state == STATE_DEAD1) return;
  114.  
  115.   if (state == STATE_DEAD2)
  116.   {
  117.     state = STATE_NORMAL;
  118.     return;
  119.   }
  120.  
  121.   if (paused)
  122.   {
  123.     paused = 0;
  124.     Mprint ("");
  125.     return;
  126.   }
  127.   spec[k] = 0x81;
  128. }
  129.  
  130. static void SpecUp (int k, int x, int y)
  131. {
  132.   spec[k] &= 0x7f;
  133. }
  134.  
  135. int KeyState (int k)
  136. /*
  137.  *  bit 7 will be set if key is down now
  138.  *  bit 0 will be set if key has been pressed since last time
  139.  *        we were asked
  140.  */
  141. {
  142.   int j;
  143.  
  144.   j = key[k];
  145.  
  146.   /* Clear since-last-time bit */
  147.   #ifndef AMIGA
  148.   key[k] &= 0xfe;
  149.   #else
  150.   key[k] = 0;
  151.   #endif /* AMIGA */
  152.  
  153.   return (j);
  154. }
  155.  
  156. int SpecKeyState (int k)
  157. /*
  158.  *  bit 7 will be set if key is down now
  159.  *  bit 0 will be set if key has been pressed since last time
  160.  *        we were asked
  161.  */
  162. {
  163.   int j;
  164.  
  165.   j = spec[k];
  166.  
  167.   /* Clear since-last-time bit */
  168.   #ifndef AMIGA
  169.   spec[k] &= 0xfe;
  170.   #else
  171.   spec[k] = 0;
  172.   #endif /* AMIGA */
  173.  
  174.   return (j);
  175. }
  176.  
  177. void InitKeyboard()
  178. /*
  179.  *  Initialize the keyboard
  180.  */
  181. {
  182.   int i;
  183.  
  184.   /* Clear all the key-pressed flags */
  185.   for (i=0; i<256; i++)
  186.   {
  187.     key[i] = 0;
  188.     spec[i] = 0;
  189.   }
  190.  
  191.   /* Set up call backs */
  192.   glutKeyboardFunc (Key);
  193.   glutSpecialFunc (Spec);
  194.   glutKeyboardUpFunc (KeyUp);
  195.   glutSpecialUpFunc (SpecUp);
  196. }
  197.  
  198. void Keyboard()
  199. /*
  200.  *  Read the keyboard
  201.  */
  202. {
  203.   int k, i;
  204.  
  205.   /* If we're waiting for Sparky to hit a key to load a game,
  206.     check the appropriate digit keys */
  207.   if (state == STATE_LOADGAME)
  208.   {
  209.     for (k=0; k<nsaves; k++)
  210.     {
  211.       if (1 & KeyState ('0' + k))
  212.       LoadGameByKey (k);
  213.     }
  214.   }
  215.  
  216.   if (KeyState (27)) /* Escape */
  217.   {
  218.     if (strcasecmp (gamemode, "no")) glutLeaveGameMode();
  219.  
  220.     /* Rewrite the preferences file before leaving */
  221.     Log ("Escape key, exiting...");
  222.     ShutdownNetwork();
  223.     FinishSound();
  224.     WritePrefs();
  225.     CloseLog();
  226.     #ifdef AMIGA
  227.     FreeJoy();
  228.     if (LucyPlayBase) CloseLibrary(LucyPlayBase);
  229.     #endif
  230.     exit (0);
  231.   }
  232.  
  233.   if (KeyState ('q'))
  234.   {
  235.     if (strcasecmp (gamemode, "no")) glutLeaveGameMode();
  236.     Log ("Quitting...");
  237.     ShutdownNetwork();
  238.     FinishSound();
  239.     WritePrefs();
  240.     CloseLog();
  241.     #ifdef AMIGA
  242.     FreeJoy();
  243.     if (LucyPlayBase) CloseLibrary(LucyPlayBase);
  244.     #endif
  245.     exit (0);
  246.   }
  247.  
  248.   if (KeyState ('Q'))
  249.   {
  250.     if (strcasecmp (gamemode, "no")) glutLeaveGameMode();
  251.     Log ("Quitting. no save...");
  252.     ShutdownNetwork();
  253.     FinishSound();
  254.     CloseLog();
  255.     #ifdef AMIGA
  256.     FreeJoy();
  257.     if (LucyPlayBase) CloseLibrary(LucyPlayBase);
  258.     #endif
  259.     exit (0);
  260.   }
  261.  
  262.   player.move_right =
  263.   player.move_left =
  264.   player.move_up =
  265.   player.move_down =
  266.   player.move_forward =
  267.   player.move_backward =
  268.   player.move_pitchleft =
  269.   player.move_pitchright = 0.0;
  270.   warpspeed = 0;
  271.  
  272.   if (SpecKeyState (GLUT_KEY_RIGHT))  player.move_right = 0.5;
  273.   if (SpecKeyState (GLUT_KEY_LEFT))   player.move_left = 0.5;
  274.   if (SpecKeyState (GLUT_KEY_UP))     player.move_up = 0.5;
  275.   if (SpecKeyState (GLUT_KEY_DOWN))   player.move_down = 0.5;
  276.   if (KeyState('a'))                  player.move_forward = 0.75;
  277.   if (KeyState('z'))                  player.move_backward = 0.75;
  278.   if (KeyState(','))                  player.move_pitchleft = 1.0;
  279.   if (KeyState('.'))                  player.move_pitchright = 1.0;
  280.  
  281.   if (KeyState ('A'))
  282.   {
  283.     player.move_forward = 0.75;
  284.     warpspeed = 1;
  285.   }
  286.  
  287.   if (KeyState ('Z'))
  288.   {
  289.     player.move_backward = 0.75;
  290.     warpspeed = 1;
  291.   }
  292.  
  293.   if (1 & KeyState ('h'))
  294.   {
  295.     drawhud = !drawhud;
  296.   }
  297.  
  298.   if (1 & KeyState ('s'))
  299.   {
  300.     switch (starfield)
  301.     {
  302.       case 0: starfield = 1;
  303.       star_list = star_list_sparse;
  304.       Cprint ("SPARSE Starfield");
  305.       break;
  306.  
  307.       case 1: starfield = 2;
  308.       star_list = star_list_dense;
  309.       Cprint ("DENSE Starfield");
  310.       break;
  311.  
  312.       case 2: starfield = 0;
  313.       Cprint ("Starfield OFF");
  314.       break;
  315.     }
  316.   }
  317.  
  318.   if (1 & KeyState (' '))
  319.   {
  320.     if (fullstop)
  321.     {
  322.       player.vel[0] = player.vel[1] = player.vel[2] = 0.0;
  323.       player.throttle = 0.0;
  324.       if (am_client) clientme.urgent = 1;
  325.       QueuePositionReport();
  326.     }
  327.   }
  328.  
  329.   if (1 & KeyState ('c'))
  330.   {
  331.     for (i=0; i<console.next; i++) console.age[i] = 0.0;
  332.   }
  333.  
  334.   if (1 & KeyState ('g'))
  335.   {
  336.     if (!am_client)
  337.     {
  338.       gravity = !gravity;
  339.       if (gravity)
  340.       {
  341.         Cprint ("Gravity ON");
  342.       }
  343.       else
  344.       {
  345.         Cprint ("Gravity OFF");
  346.       }
  347.       if (am_server) SendFlags();
  348.     }
  349.   }
  350.  
  351.   if (1 & KeyState ('j'))
  352.   {
  353.     junk = !junk;
  354.     if (junk)
  355.     Cprint ("Space junk ON");
  356.     else
  357.     Cprint ("Space junk OFF");
  358.   }
  359.  
  360.   if (1 & KeyState ('T'))
  361.   {
  362.     joy_throttle = !joy_throttle;
  363.     if (joy_throttle)
  364.     Cprint ("Joystick throttle ON");
  365.     else
  366.     Cprint ("Joystick throttle OFF");
  367.   }
  368.  
  369.   if (1 & KeyState ('e'))
  370.   {
  371.     sound = !sound;
  372.     if (sound)
  373.     Cprint ("Sound effects ON");
  374.     else
  375.     Cprint ("Sound effects OFF");
  376.   }
  377.  
  378.   if (1 & KeyState ('i'))
  379.   {
  380.     if (!am_client && !am_server)
  381.     {
  382.       vulnerable = !vulnerable;
  383.       if (vulnerable)
  384.       Cprint ("Vulnerable");
  385.       else
  386.       Cprint ("Invulnerable");
  387.     }
  388.   }
  389.  
  390.   if (1 & KeyState ('P'))
  391.   {
  392.     ScreenShot();
  393.   }
  394.  
  395.   if (1 & KeyState ('n'))
  396.   {
  397.     show_names = !show_names;
  398.     if (show_names)
  399.     Cprint ("Names ON");
  400.     else
  401.     Cprint ("Names OFF");
  402.   }
  403.  
  404.   if (1 & KeyState ('m'))
  405.   {
  406.     if (message.age < MSG_MAXAGE)
  407.     message.age = MSG_MAXAGE + 1.0;
  408.     else
  409.     message.age = 0.0;
  410.   }
  411.  
  412.   if (1 & KeyState ('x'))
  413.   {
  414.     textures = !textures;
  415.     if (textures)
  416.     Cprint ("Textures ON");
  417.     else
  418.     Cprint ("Textures OFF");
  419.   }
  420.  
  421.   if (1 & KeyState ('r'))
  422.   {
  423.     rings = !rings;
  424.     if (rings)
  425.     Cprint ("Rings ON");
  426.     else
  427.     Cprint ("Rings OFF");
  428.   }
  429.  
  430.   if (1 & KeyState ('u')) LockNearest();
  431.  
  432.   if (1 & KeyState ('y')) LockNext();
  433.  
  434.   if (1 & KeyState ('Y')) LockPrev();
  435.  
  436.   if (1 & KeyState ('b'))
  437.   {
  438.     if (!am_client && !am_server) Mprint (mission.briefing);
  439.   }
  440.  
  441.   if (1 & KeyState ('w'))
  442.   {
  443.     player.weapon = (player.weapon + 1) % NPLAYER_WEAPONS;
  444.   }
  445.  
  446.   if (1 & KeyState ('W'))
  447.   {
  448.     player.weapon = (player.weapon + NPLAYER_WEAPONS - 1) %
  449.     NPLAYER_WEAPONS;
  450.   }
  451.  
  452.   if (1 & KeyState ('1')) player.weapon = 0;
  453.   if (1 & KeyState ('2')) player.weapon = 1;
  454.   if (1 & KeyState ('3')) player.weapon = 2;
  455.   if (1 & KeyState ('4')) player.weapon = 3;
  456.  
  457.   if (1 & KeyState ('p'))
  458.   {
  459.     if (!am_client && !am_server)
  460.     {
  461.       Mprint ("Paused");
  462.       paused = 1;
  463.     }
  464.   }
  465.  
  466.   /* Control-P for silent pause */
  467.   if (1 & KeyState (16))
  468.   {
  469.     if (!am_client && !am_server) paused = 1;
  470.   }
  471.  
  472.   if (1 & KeyState ('M'))
  473.   {
  474.     mouse_control = !mouse_control;
  475.     if (mouse_control)
  476.     {
  477.       Cprint ("Mouse ON");
  478.       InitMouse();
  479.     }
  480.     else
  481.     {
  482.       Cprint ("Mouse OFF");
  483.       glutSetCursor (GLUT_CURSOR_INHERIT);
  484.     }
  485.   }
  486.  
  487.   if (KeyState ('\t')) PlayerFires();
  488.  
  489.   if (1 & KeyState ('l'))
  490.   {
  491.     lock.type = (lock.type + 1) % 3;
  492.     lock.target = -1;
  493.     LockNearest();
  494.  
  495.     switch (lock.type)
  496.     {
  497.       case LOCK_ENEMY:
  498.       Cprint ("Locking ENEMIES");
  499.       break;
  500.  
  501.       case LOCK_FRIENDLY:
  502.       Cprint ("Locking FRIENDLIES");
  503.       break;
  504.  
  505.       case LOCK_PLANET:
  506.       Cprint ("Locking PLANETS");
  507.       break;
  508.     }
  509.   }
  510.  
  511.   if (1 & KeyState ('f'))
  512.   {
  513.     if (!am_client)
  514.     {
  515.       player.flightmodel = (player.flightmodel + 1) % 2;
  516.  
  517.       if (player.flightmodel == FLIGHT_NEWTONIAN)
  518.       Cprint ("NEWTONIAN flight model");
  519.       else
  520.       Cprint ("ARCADE flight model");
  521.  
  522.       if (am_server) SendFlags();
  523.     }
  524.   }
  525.  
  526.   if (1 & KeyState ('L'))
  527.   {
  528.     if (!am_client && !am_server) LoadGame();
  529.   }
  530.  
  531.   if (1 & KeyState (']')) NextWaypoint();
  532.   if (1 & KeyState ('[')) PrevWaypoint();
  533.  
  534.   if (1 & KeyState ('F'))
  535.   {
  536.     showfps = !showfps;
  537.  
  538.     if (showfps)
  539.     Cprint ("Framerate ON");
  540.     else
  541.     Cprint ("Framerate OFF");
  542.   }
  543.  
  544.   if (1 & KeyState ('S'))
  545.   {
  546.     if (am_client)
  547.     {
  548.       Mprint ("Alread a client");
  549.     }
  550.     else if (am_server)
  551.     {
  552.       ShutdownServer();
  553.     }
  554.     else
  555.     {
  556.       BecomeServer();
  557.     }
  558.   }
  559.  
  560.   if (1 & KeyState ('C'))
  561.   {
  562.     if (am_client)
  563.     {
  564.       ShutdownClient();
  565.     }
  566.     else if (am_server)
  567.     {
  568.       Mprint ("Already a server");
  569.     }
  570.     else
  571.     {
  572.       GetText ("Connect to: ", DoConnect);
  573.     }
  574.   }
  575.  
  576.   if (1 & KeyState ('U')) ShowClients();
  577.   if (1 & SpecKeyState (GLUT_KEY_F1)) ShowClients();
  578.  
  579.   if (1 & KeyState ('>'))
  580.   {
  581.     if (fov < 180.0)
  582.     {
  583.       fov += 5.0;
  584.       Reshape (ScreenWidth, ScreenHeight);
  585.     }
  586.     Cprint ("Field of view %3.0lf", fov);
  587.   }
  588.  
  589.   if (1 & KeyState ('<'))
  590.   {
  591.     if (fov > 5.0)
  592.     {
  593.       fov -= 5.0;
  594.       Reshape (ScreenWidth, ScreenHeight);
  595.     }
  596.     Cprint ("Field of view %3.0lf", fov);
  597.   }
  598.  
  599.   if (1 & KeyState ('t'))
  600.   {
  601.     GetText ("Say: ", DoChat);
  602.   }
  603.  
  604.   if (1 & KeyState (4)) /* control-D */
  605.   {
  606.     if (am_server) GetText ("Drop client: ", DoDrop);
  607.   }
  608.  
  609.   if (1 & KeyState (12)) /* control-L */
  610.   {
  611.     if (!am_server && !am_client) GetText ("Load mission: ", DoLoad);
  612.   }
  613.  
  614.   if (1 & KeyState (14)) /* control-N */
  615.   {
  616.     if (!am_client && !am_server)
  617.     {
  618.       GetText ("Player name: ", DoName);
  619.     }
  620.   }
  621.  
  622.   if (1 & KeyState ('o'))
  623.   {
  624.     draw_orbits = !draw_orbits;
  625.  
  626.     if (draw_orbits)
  627.     Cprint ("Orbits ON");
  628.     else
  629.     Cprint ("Orbits OFF");
  630.   }
  631.  
  632.   if (1 & KeyState ('O'))
  633.   {
  634.     if (!am_client && !am_server)
  635.     {
  636.       orbit = !orbit;
  637.  
  638.       if (orbit)
  639.       Cprint ("Orbiting ON");
  640.       else
  641.       Cprint ("Orbiting OFF");
  642.     }
  643.   }
  644.  
  645.   if (1 & KeyState ('k'))
  646.   {
  647.     if (!am_client && !am_server)
  648.     {
  649.       compression *= 2.0;
  650.       PrintCompression();
  651.     }
  652.   }
  653.  
  654.   if (1 & KeyState ('K'))
  655.   {
  656.     if (!am_client && !am_server)
  657.     {
  658.       compression /= 2.0;
  659.       PrintCompression();
  660.     }
  661.   }
  662.  
  663.   if (1 & KeyState ('v'))
  664.   {
  665.     player.viewlock = !player.viewlock;
  666.  
  667.     if (player.viewlock)
  668.     Cprint ("View LOCKED");
  669.     else
  670.     Cprint ("View UNLOCKED");
  671.   }
  672.  
  673.   if (1 & KeyState ('{'))
  674.   {
  675.     clipnear *= 0.75;
  676.     Cprint ("Near = %lf", clipnear);
  677.     Reshape (ScreenWidth, ScreenHeight);
  678.   }
  679.   if (1 & KeyState ('}'))
  680.   {
  681.     clipnear *= 1.25;
  682.     Cprint ("Near = %lf", clipnear);
  683.     Reshape (ScreenWidth, ScreenHeight);
  684.   }
  685.   if (1 & KeyState ('('))
  686.   {
  687.     clipfar *= 0.75;
  688.     Cprint ("Far = %lf", clipfar);
  689.     Reshape (ScreenWidth, ScreenHeight);
  690.   }
  691.   if (1 & KeyState (')'))
  692.   {
  693.     clipfar *= 1.25;
  694.     Cprint ("Far = %lf", clipfar);
  695.     Reshape (ScreenWidth, ScreenHeight);
  696.   }
  697. }
  698.  
  699. void PrintCompression()
  700. {
  701.   if (compression >= 1.0)
  702.   {
  703.     Cprint ("Compression %.0lfX", compression);
  704.   }
  705.   else
  706.   {
  707.     Cprint ("Compression %lfX", compression);
  708.   }
  709. }
  710.